<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MyProviderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $authorizations = \Auth::user()->purchaserAuthorizationRequests()->display();

        if ($name = $request->query('provider')) {
            $authorizations->whereHas('provider.information', function ($query) use ($name) {
                $query->where('company', 'like', "%$name%");
            });
        }

        if ($status = $request->query('status')) {
            if ($status == 1) {
                $authorizations->available();
            } else {
                $authorizations->where('status', 2)->where('authorize_stop', '<', date('Y-m-d', time()));
            }
        }

        $authorizations = $authorizations->orderBy('created_at', 'desc')->get();

        $data = [];

        foreach ($authorizations as $authorization) {
            $data[] = [
                'id'                => $authorization->id,
                'provider'          => $authorization->provider->information->company,
                'request_time'      => date('Y-m-d', strtotime($authorization->authorize_start)),
                'request_timestamp' => strtotime($authorization->authorize_start),
                'stop_time'         => date('Y-m-d', strtotime($authorization->authorize_stop)),
                'stop_timestamp'    => strtotime($authorization->authorize_stop),
                'amount'            => $authorization->licensedGoods()->count(),
                'status'            => $authorization->getAuthorizationStatus() + 1,
            ];
        }

        return success_return(['list' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
